import os
import sys

dirfiles = os.listdir('stats')


fieldmap = {}


fieldmap['[P] Flush Interval']=2
fieldmap['[P] Mod Index']=3
fieldmap['[P] Parity Group Size']=4
fieldmap['[P] Parity Model']=5
fieldmap['[P] Use Memory for Parity']=6
fieldmap['Average Seek Time']=7
fieldmap['Completely Iddle Time']=8   
fieldmap['Completely Iddle Time Percentage']=9
fieldmap['Inter-arrival Time Average']=10
fieldmap['Inter-arrival Time Standard Deviation']=11
fieldmap['Max Memory Usage']=12
fieldmap['Maximum queue length']=13
fieldmap['Memory Hits']=14
fieldmap['Number of Flushes']=15
fieldmap['Original Requests Count']=16
fieldmap['Parity Requests Count']=17
fieldmap['Reads']=18
fieldmap['Reads Percentage']=19
fieldmap['Response Time Mean (excluding parity requests)']=20
fieldmap['Response Time Variance (exclusing parity requests)']=21
fieldmap['Sequential Reads']=22
fieldmap['Sequential Reads Percentage']=23
fieldmap['Sequential Writes']=24
fieldmap['Sequential Writes Percentage']=25
fieldmap['Simulation Time']=26
fieldmap['System Queue time average']=27
fieldmap['Total Requests Handled']=28
fieldmap['Writes']=29
fieldmap['Writes Percentage']=30


wh = open('final.csv','w')

headers = ['' for x in xrange(31)]
headers[0] = 'Scheduler'
headers[1] = 'Trace'
for x in fieldmap:
    headers[fieldmap[x]] = x.replace(' ','_')
#headers[31] = 'Filename'

print headers

wh.write(",".join(headers) + "\n")


print len(dirfiles)

for fname in dirfiles:
    if fname.find('out') >= 0:
        csl = ['N/A' for x in xrange(31)]
        fh = open("stats/" + fname)
        if fname.find('self2') > 0:
            csl[0] = 'SSTF'
        else:
            csl[0] = 'CYCLE'

        x = fname.find('.cfg')
        csl[1] = fname[x-1]
        #csl[31] = fname

        lines = fh.readlines()
        lines = [l.strip() for l in lines]
        for l in lines:
            aux = l.split(':')
            csl[fieldmap[aux[0].strip()]] = aux[1].strip()

        wh.write(",".join(csl) + "\n")


        fh.close()

wh.close()
